package com.navajo.web.spring.eventbus;

import java.lang.reflect.Method;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Service;

import com.google.common.eventbus.EventBus;
import com.google.common.eventbus.Subscribe;
import com.navajo.web.core.ScopeVaadin;

//@Service
public class EventBusSubscriberRegisterar implements BeanPostProcessor,Ordered {
    private final EventBus eventBus;



    @Autowired
    public EventBusSubscriberRegisterar(EventBus eventBus) {
        this.eventBus = eventBus;
    }



    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        return bean;
    }



    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        
        System.out.println("EventBusSubscriberRegisterar.postProcessAfterInitialization():" + beanName);
        
        
        for (Method method : bean.getClass().getMethods()) {
            if (method.getAnnotation(Subscribe.class) != null) {
        
                eventBus.register(bean);
              //  break;
            }
        }
        return bean;
    }



    @Override
    public int getOrder() {

        return Ordered.HIGHEST_PRECEDENCE;
    }
}